home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / ML_VECTB.ZIP / SOURCE / ENGINE3D.PAS < prev    next >
Pascal/Delphi Source File  |  1996-03-10  |  5KB  |  161 lines

  1. Unit Engine3d;
  2. {
  3.   3D Engine, v3.0
  4.   by Maple Leaf, March 1996
  5.   No fuckin rights reserved.
  6. }
  7.  
  8. Interface
  9.  
  10. Const
  11.   Perspective   : Boolean = True;  { Default with perspective }
  12.   ZoomFactor    : LongInt = 220;   { Zooming 220% as default  }
  13.   PiOver180     : Real    = Pi/180;
  14.   CenterX       : LongInt = 160;   { Center's coordinates     }
  15.   CenterY       : LongInt = 100;
  16.   ObserverX     : LongInt = 0;     { Defaults. See procedure SetObserver.. }
  17.   ObserverY     : LongInt = 0;
  18.   ObserverDist  : LongInt = 300;   { Initial focus }
  19.   CameraX       : Longint = 0;     { Initial camera coords in 3D space }
  20.   CameraY       : Longint = 0;
  21.   CameraZ       : Longint = 0;
  22.  
  23. Var
  24.   _3DX          : LongInt;         {-----------}
  25.   _3DY          : LongInt;         { 3D coords }
  26.   _3DZ          : LongInt;         {-----------}
  27.   _2DX          : LongInt;         { 2D X      }
  28.   _2DY          : LongInt;         { 2D Y      }
  29.   { ---- Angles ---- }
  30.   RotAngle, TiltAngle       : Integer;
  31.   CosA, CosB, SinA, SinB    : Real;
  32.   CosTab, SinTab            : Array [0..359] of LongInt; { Integer(cosinus|sinus*256) }
  33.   Xt, Yt, Zt, TmpEqu : LongInt;
  34.  
  35. Procedure SetCenter (x,y:Integer);
  36. { Sets the center of the screen }
  37. Procedure SetAngles (Rotation,Tilt:Integer);
  38. { Sets the angles (horizontal rotation and tilt) }
  39. Procedure SetObserverPosition (x,y:LongInt; Focus:LongInt);
  40. { Sets the focus of the camera (e.g. the distance between the
  41.   observer and the projection plane), and the 2D position (X,Y)
  42.   of the observer in a plane parallel w/ the projection plane. }
  43. Procedure SetCamera (x,y,z:Longint);
  44. { Sets the (X,Y,Z) position of the camera in the virtual 3D space }
  45. { Or else: (X,Y,Z) will be the only point which has a FIXED position
  46.   onto the 2D desktop (e.g. screen), for any rotations }
  47. Procedure MapCoordinates;
  48. { Default Floating-Point mapping routine. Translates a 3D point into a 2D one. }
  49. Procedure IntMapCoordinates;
  50. { Default integer-mapping routine. Translates a 3D point into a 2D one. }
  51. Procedure IntMapCoordinates2;
  52. { Second integer-mapping routine. Uses ZoomFactor=256=constant, for speed. }
  53. Procedure IncrAngle (var Angle:Integer; value:integer);
  54. { Increment or decrement the specified angle w/ a specified
  55.   value, taking care of translating the result in the interval [0-359] }
  56.  
  57. Implementation
  58. {$L engine3d}
  59.  
  60. Procedure IntMapCoordinates;external;
  61. Procedure IntMapCoordinates2;external;
  62.  
  63. Procedure SetCenter(x,y:Integer);assembler;
  64. asm
  65.   mov ax,x
  66.   db 66h; cbw
  67.   db 66h; mov word ptr CenterX,ax  { Screen center (X coordinate) }
  68.   mov ax,y
  69.   db 66h; cbw
  70.   db 66h; mov word ptr CenterY,ax  { Screen center (Y coordinate) }
  71. end;
  72.  
  73. Procedure SetAngles(Rotation,Tilt:Integer);
  74. begin
  75.   RotAngle:=Rotation;
  76.   TiltAngle:=Tilt;
  77.   IncrAngle(RotAngle,0);
  78.   IncrAngle(TiltAngle,0);
  79. end;
  80.  
  81. Procedure SetObserverPosition(x,y:LongInt; Focus:LongInt);assembler;
  82. asm
  83.   db 66h; mov ax,word ptr x
  84.   db 66h; neg ax
  85.   db 66h; mov word ptr ObserverX,ax     { Observer's X position }
  86.   db 66h; mov ax,word ptr y
  87.   db 66h; neg ax
  88.   db 66h; mov word ptr ObserverY,ax     { Observer's Y position }
  89.   db 66h; mov ax,word ptr Focus
  90.   db 66h; mov word ptr ObserverDist,ax  { Distance from the observer to the projection plane (z=0) }
  91. end;
  92.  
  93. Procedure MapCoordinates;
  94. var Xt,Yt,Zt  : Real;
  95.     OneOverZt : Real;
  96. begin
  97.   { Init some vars, for speed }
  98.   CosA:=Cos( RotAngle * PiOver180 );
  99.   SinA:=Sin( RotAngle * PiOver180 );
  100.   CosB:=Cos( TiltAngle* PiOver180 );
  101.   SinB:=Sin( TiltAngle* PiOver180 );
  102.   { Init camera (bring it to the virtual screen's center) }
  103.   _3dx:=_3dx-CameraX;
  104.   _3dy:=_3dy-CameraY;
  105.   _3dz:=_3dz-CameraZ;
  106.   { Calculations }
  107.   Xt:= ObserverX + _3DX*CosA - _3DY*SinA;
  108.   Yt:= ObserverY + (_3DX*SinA+_3DY*CosA)*SinB + _3DZ*CosB;
  109.   if Perspective then begin
  110.     Zt:= ObserverDist + (_3DX*SinA+_3DY*CosA)*CosB - _3DZ*SinB;
  111.     _2DX:=CenterX+Trunc(Xt*ZoomFactor/Zt);
  112.     _2DY:=CenterY-Trunc(Yt*ZoomFactor/Zt*13/16);
  113.   end else begin
  114.     _2DX:=CenterX+Trunc(Xt);        { Faster, but less precise in aspect - no running points }
  115.     _2DY:=CenterY-Trunc(Yt*13/16);  { Faster, but less precise in aspect }
  116.   end;
  117. end;
  118.  
  119. Procedure IncrAngle(var Angle:Integer; value:integer);assembler;
  120. asm
  121.   les di,Angle
  122.   mov ax,value
  123.   mov cx,es:[di]
  124.   add cx,ax
  125.   or cx,cx
  126.   jl @Under
  127.   cmp cx,359
  128.   jg @Above
  129.   mov es:[di],cx
  130.   jmp @Ok
  131. @Above:
  132.   sub cx,360
  133.   mov es:[di],cx
  134.   jmp @Ok
  135. @Under:
  136.   mov ax,360
  137.   add ax,cx
  138.   mov es:[di],ax
  139. @Ok:
  140. end;
  141.  
  142. Procedure SetCamera(x,y,z:longint);assembler;
  143. asm
  144.   db 66h; mov ax,word ptr x
  145.   db 66h; mov word ptr CameraX,ax
  146.   db 66h; mov ax,word ptr y
  147.   db 66h; mov word ptr CameraY,ax
  148.   db 66h; mov ax,word ptr z
  149.   db 66h; mov word ptr CameraZ,ax
  150. end;
  151.  
  152. var k:word;
  153.  
  154. Begin
  155.   { Constructing default COS and SIN tables ... }
  156.   for k:=0 to 359 do begin
  157.     CosTab[k]:=LongInt(Trunc(cos(k*pi/180)*256));
  158.     SinTab[k]:=LongInt(Trunc(sin(k*pi/180)*256));
  159.   end;
  160. End.
  161.